#include "StdAfx.h"
#include ".\imagectrl.h"

//##ModelId=44C4B6050167
CJoyImageCtrl::CJoyImageCtrl(void)
{
    m_invertX   = false;
    m_invertY   = false;
    m_recvMouse = false;
    m_skinArea  = true;
}

//##ModelId=44C4B6050169
CJoyImageCtrl::~CJoyImageCtrl(void)
{
}

//##ModelId=44C4B605014E
void CJoyImageCtrl::CM_LoadSkin(CJoyXmlElement* pSkinElem)
{
    __super::CM_LoadSkin(pSkinElem);

    CJoyXmlAttribute* pImageAttrib = pSkinElem->FindAttribute("image");
    if(pImageAttrib != NULL)
    {
        if(!pImageAttrib->m_value.IsEmpty())
        {
            SetImage(pImageAttrib->m_value);
        }
    }
}

//##ModelId=44C4B605016C
void CJoyImageCtrl::SetImage(PCSTR pszName)
{
    m_image = JoyGetManager()->CreateImage(pszName);
    if(m_image == NULL)
    {
        return;
    }
    SetArea(CRect(
        m_area.left,
        m_area.top,
        m_area.left+m_image->GetWidth(),
        m_area.top+m_image->GetHeight()));
}

//##ModelId=44C4B6050158
void CJoyImageCtrl::CM_Draw(Graphics& graph)
{
    if(m_image == NULL)
    {
        return;
    }

    Rect src;
    INT w = m_image->GetWidth();
    INT h = m_image->GetHeight();
    if(m_invertX)
    {
        src.X       = w;
        src.Width   = -w;
    }
    else
    {
        src.X       = 0;
        src.Width   = w;
    }
    if(m_invertY)
    {
        src.Y       = h;
        src.Height  = -h;
    }
    else
    {
        src.Y       = 0;
        src.Height  = h;
    }

    graph.DrawImage(m_image, Rect(0, 0, w, h),
        src.X, src.Y, src.Width, src.Height, UnitPixel);
}
